﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject.Business;
using System.Data;

namespace eProject.Web.UI
{
    public partial class AirportsManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateControls();
            PopulateLabels();
        }

        private void PopulateControls()
        {
            if (Page.IsPostBack) return;
            BindAirports();
        }

        private void PopulateLabels()
        {
            Title = "Quản trị Airport- eProject";
        }

        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(this.Page_Load);
            this.btnAddNew.Click += new EventHandler(btnAddNew_Click);
            grvAirports.RowCommand += new GridViewCommandEventHandler(grvAirports_RowCommand);
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            grvAirports.ShowFooter = true;
            BindAirports();
        }

        protected void grvAirports_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("CancelInsert"))
            {
                grvAirports.ShowFooter = false;
                BindAirports();
            }
            if (e.CommandName.Equals("EmptyInsert"))
            {
                GridViewRow row = (GridViewRow)((e.CommandSource as ImageButton).NamingContainer);
                TextBox AirportID = row.FindControl("txtAirportID") as TextBox;
                TextBox Name = row.FindControl("txtPlaneName") as TextBox;
                DropDownList CityId = row.FindControl("ddlCityID") as DropDownList;
                AirPorts airport = new AirPorts();
                airport.AirPortId = AirportID.Text.ToString().Trim();
                airport.Name = Name.Text.ToString().Trim();
                airport.CityId = Convert.ToInt32(CityId.SelectedValue.ToString());
                if (airport.Save())
                {
                    grvAirports.ShowFooter = false;
                    BindAirports();
                }
            }
            if (e.CommandName.Equals("Insert"))
            {
                TextBox AirportID = grvAirports.FooterRow.FindControl("txtAirportID") as TextBox;
                TextBox Name = grvAirports.FooterRow.FindControl("txtPlaneName") as TextBox;
                DropDownList CityId = grvAirports.FooterRow.FindControl("ddlCityID") as DropDownList;
                AirPorts airport = new AirPorts();
                airport.AirPortId = AirportID.Text.ToString().Trim();
                airport.Name = Name.Text.ToString().Trim();
                airport.CityId = Convert.ToInt32(CityId.SelectedValue.ToString());
                //airport.CreatedDate = DateTime.Now;
                //airport.LastUpdate = DateTime.Now;
                if (airport.Save())
                {
                    grvAirports.ShowFooter = false;
                    BindAirports();
                }
            }
        }

        private void BindAirports()
        {
            try
            {
                grvAirports.DataSource = AirPorts.GetAll();
                grvAirports.DataBind();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public List<Cities> BindCities()
        {
            return Cities.GetAll();
        }

        protected void grvAirports_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                grvAirports.EditIndex = e.NewEditIndex;
                BindAirports();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        protected void grvAirports_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                int id = int.Parse(grvAirports.DataKeys[e.RowIndex].Value.ToString());
                AirPorts airport = new AirPorts(id);
                airport.AirPortId = ((TextBox)grvAirports.Rows[e.RowIndex].FindControl("txtAirportID")).Text.Trim();
                airport.Name = ((TextBox)grvAirports.Rows[e.RowIndex].FindControl("txtPlaneName")).Text.Trim();
                airport.CityId = Convert.ToInt32(((DropDownList)grvAirports.Rows[e.RowIndex].FindControl("ddlCityID")).SelectedValue.ToString());
                airport.LastUpdate = DateTime.Now;
                airport.Save();
                grvAirports.EditIndex = -1;
                BindAirports();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        protected void grvAirports_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                grvAirports.EditIndex = -1;
                BindAirports();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        protected void grvAirports_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void grvAirports_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvAirports.PageIndex = e.NewPageIndex;
            grvAirports.DataBind();
            BindAirports();
        }
    }
}